/*
 * Created by JFormDesigner on Fri Nov 08 21:37:46 CST 2024
 */

package com.hafu.view.ShangPin;

import com.hafu.model.SpInfo;
import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;

import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.DefaultTableModel;

/**
 * @author ddd
 */
public class MangeShangPinFrm extends JFrame {
    private SpInfoService spInfoService = new SpInfoService();
    public MangeShangPinFrm() {
        initComponents();
        fillTable();

    }
    private void fillTable() {
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        dtn.setRowCount(0);
        ArrayList<SpInfo> spInfos = spInfoService.getSpInfoByName(new SpInfo());
        for (SpInfo spInfo : spInfos) {
            Object[] rowData = {spInfo.getId(), spInfo.getName(), spInfo.getPrice(), spInfo.getSellPrice()};
            dtn.addRow(rowData);
        }
    }

    private void button1(ActionEvent e) {
        // TODO add your code here
        String name = textField1.getText();
        SpInfo spInfo= new SpInfo(name);
        ArrayList<SpInfo> spInfos = spInfoService.getSpInfoByName(spInfo);
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        dtn.setRowCount(0);
        if (spInfos.size() > 0){

            for (SpInfo spInfo1 : spInfos) {
                Object[] rowData = {spInfo1.getId(), spInfo1.getName(), spInfo1.getPrice(), spInfo1.getSellPrice()};
                dtn.addRow(rowData);
            }

        }else {
            JOptionPane.showMessageDialog(null,"没有找到该商品");
        }
    }

    private void table1MouseClicked(MouseEvent e) {
        // TODO add your code here
        int row = table1.getSelectedRow();
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtn.getValueAt(row, 0);
        String name = (String) dtn.getValueAt(row, 1);
        BigDecimal price = (BigDecimal) dtn.getValueAt(row, 2);
        BigDecimal sellPrice = (BigDecimal) dtn.getValueAt(row, 3 );
        textField2.setText(name);
        textField3.setText(String.valueOf(price));
        textField4.setText(String.valueOf(sellPrice));
    }

    private void button2(ActionEvent e) {
        // TODO add your code here
        String name = textField2.getText();
        String price = textField3.getText();
        String sellPrice = textField4.getText();
        if (StringUtil.isEmpty(name)){
            JOptionPane.showMessageDialog(null,"商品名不能为空");
            return;
        }
        if (StringUtil.isEmpty(price)){
            JOptionPane.showMessageDialog(null,"商品进价为空");
            return;
        }
        if (StringUtil.isEmpty(sellPrice)){
            JOptionPane.showMessageDialog(null,"商品售价不能为空");
            return;
        }
        BigDecimal price1 = new BigDecimal(price);
        BigDecimal sellPrice1 = new BigDecimal(sellPrice);
        int row = table1.getSelectedRow();
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtn.getValueAt(row, 0);
        SpInfo spInfo= new SpInfo(name,price1,sellPrice1);
        Boolean aBoolean = spInfoService.updateSpInfo(spInfo);
        if (aBoolean){
            JOptionPane.showMessageDialog(null,"修改成功");
            fillTable();
        }else {
            JOptionPane.showMessageDialog(null,"修改失败");
        }


    }
        private void button3(ActionEvent e) {
        // TODO add your code here  删除事件
        int row = table1.getSelectedRow();
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        int id = (int) dtn.getValueAt(row, 0);
        Boolean isdeleteSpInfo = spInfoService.deleteSpInfo(id);
        if (isdeleteSpInfo){
            JOptionPane.showMessageDialog(null,"删除成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null,"删除失败");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        button1 = new JButton();
        textField1 = new JTextField();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        textField2 = new JTextField();
        textField3 = new JTextField();
        textField4 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();

        //======== this ========
        setTitle("\u7ba1\u7406\u5546\u54c1");
        Container contentPane = getContentPane();

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null, null},
                    {"", "", "", ""},
                },
                new String[] {
                    "\u7f16\u53f7", "\u540d\u5b57", "\u8fdb\u4ef7", "\u552e\u4ef7"
                }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //---- label1 ----
        label1.setText("\u5546\u54c1\u540d\u5b57");

        //---- label2 ----
        label2.setText("\u5546\u54c1\u540d\u5b57");

        //---- label3 ----
        label3.setText("\u5546\u54c1\u8fdb\u4ef7");

        //---- label4 ----
        label4.setText("\u5546\u54c1\u552e\u4ef7");

        //---- button2 ----
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> button2(e));

        //---- button3 ----
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> button3(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(30, 131, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createParallelGroup()
                            .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGap(144, 144, 144)
                                .addComponent(label1)
                                .addGap(54, 54, 54)
                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
                                .addGap(100, 100, 100)
                                .addComponent(button1, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
                                .addGap(225, 225, 225))
                            .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                .addGroup(contentPaneLayout.createParallelGroup()
                                    .addComponent(label3)
                                    .addGroup(contentPaneLayout.createSequentialGroup()
                                        .addGroup(contentPaneLayout.createParallelGroup()
                                            .addComponent(label2)
                                            .addComponent(label4))
                                        .addGap(173, 173, 173)
                                        .addGroup(contentPaneLayout.createParallelGroup()
                                            .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
                                            .addComponent(textField3, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
                                            .addComponent(textField4, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE))
                                        .addGap(84, 84, 84)
                                        .addGroup(contentPaneLayout.createParallelGroup()
                                            .addComponent(button3)
                                            .addComponent(button2))))
                                .addGap(196, 196, 196)))
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 625, GroupLayout.PREFERRED_SIZE)
                            .addGap(111, 111, 111))))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGap(24, 24, 24)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button1))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 267, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label2))
                            .addGap(18, 18, 18)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label3)))
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addComponent(button2)
                            .addGap(24, 24, 24)))
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addComponent(button3)
                            .addGap(47, 47, 47))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(31, 31, 31)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(textField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label4))
                            .addGap(20, 20, 20))))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton button1;
    private JTextField textField1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JButton button2;
    private JButton button3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
